package com.xiamaliu.spring.aop;

import com.xiamaliu.spring.aop.aspect.GPAdvice;
import com.xiamaliu.spring.aop.support.GPAdvisedSupport;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

public class GPJdkDynamicAopProxy implements InvocationHandler {

    private GPAdvisedSupport advised;

    public GPJdkDynamicAopProxy(GPAdvisedSupport advised) {
        this.advised = advised;
    }

    public Object getProxy() {
        return getProxy(this.advised.getTargetClass().getClassLoader());
    }

    public Object getProxy(ClassLoader classLoader) {
        return Proxy.newProxyInstance(classLoader, this.advised.getTargetClass().getInterfaces(), this);
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Map<String, GPAdvice> advices = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, this.advised.getTargetClass());
        invokeAdvice(advices.get("before"));
        Object returnValue;
        try {
            returnValue = method.invoke(this.advised.getTarget(), args);
        } catch (Exception e) {
            invokeAdvice(advices.get("afterThrowing"));
            throw e;
        }
        invokeAdvice(advices.get("after"));
        return returnValue;
    }

    private void invokeAdvice(GPAdvice advice) {
        try {
            advice.getAdviceMethod().invoke(advice.getAspect());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
